package com.dds.system.service.union.impl;

import java.util.*;

import com.dds.system.domain.union.UnionShopOrder;
import com.dds.system.mapper.union.UnionShopOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dds.system.mapper.union.UnionShopRecommendMapper;
import com.dds.system.domain.union.UnionShopRecommend;
import com.dds.system.service.union.IUnionShopRecommendService;

/**
 * 联盟商家 APP前端=推荐关系Service业务层处理
 *
 * @author gaolianjie
 * @date 2021-04-24
 */
@Service
public class UnionShopRecommendServiceImpl implements IUnionShopRecommendService {
    @Autowired
    private UnionShopRecommendMapper unionShopRecommendMapper;

    @Autowired
    private UnionShopOrderMapper unionShopOrderMapper;

    /**
     * 查询联盟商家 APP前端=推荐关系
     *
     * @param shopId 联盟商家 APP前端=推荐关系ID
     * @return 联盟商家 APP前端=推荐关系
     */
    @Override
    public UnionShopRecommend selectUnionShopRecommendById(String shopId) {
        return unionShopRecommendMapper.selectUnionShopRecommendById(shopId);
    }

    /**
     * 店铺推荐列表
     * @param shopName 商家名称
     * @param time 创建时间
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectUnionShopRecommendList(String shopName, Map<String, Object> time) {
        return unionShopRecommendMapper.selectUnionShopRecommendList(shopName, time);
    }

    /**
     * 新增联盟商家 APP前端=推荐关系
     *
     * @param unionShopRecommend 联盟商家 APP前端=推荐关系
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int insertUnionShopRecommend(UnionShopRecommend unionShopRecommend, Long userId) {
        unionShopRecommend.setCreatedUser(userId);
        return unionShopRecommendMapper.insertUnionShopRecommend(unionShopRecommend);
    }

    /**
     * 修改联盟商家 APP前端=推荐关系
     *
     * @param unionShopRecommend 联盟商家 APP前端=推荐关系
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int updateUnionShopRecommend(UnionShopRecommend unionShopRecommend, Long userId) {
        unionShopRecommend.setUpdatedUser(userId);
        return unionShopRecommendMapper.updateUnionShopRecommend(unionShopRecommend);
    }

    /**
     * 批量删除联盟商家 APP前端=推荐关系
     *
     * @param shopIds 需要删除的联盟商家 APP前端=推荐关系ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopRecommendByIds(String[] shopIds) {
        return unionShopRecommendMapper.deleteUnionShopRecommendByIds(shopIds);
    }

    /**
     * 删除联盟商家 APP前端=推荐关系信息
     *
     * @param shopId 联盟商家 APP前端=推荐关系ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopRecommendById(String shopId) {
        return unionShopRecommendMapper.deleteUnionShopRecommendById(shopId);
    }

    /**
     * 增加5条推荐数据
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public boolean addNewRecommends(Long userId) {
        // 获取最新排序值
        int sort = 5;
        UnionShopRecommend nowRecommend = unionShopRecommendMapper.selectUnionShopRecommendBySortDesc();
        if (nowRecommend != null) {
            sort = nowRecommend.getSort();
        }

        List<Map<String, Object>> orderList = unionShopOrderMapper.selectUnionShopOrderListByRecommend();

        // 根据订单数量排序
        orderList.sort((o1, o2) -> {
            Integer o1C = Integer.valueOf(o1.get("c").toString());
            Integer o2C = Integer.valueOf(o2.get("c").toString());

            return o1C.compareTo(o2C);
        });

        // 删除推荐数据前5条
        unionShopRecommendMapper.deleteUnionShopRecommendBySort();

        int i = 1;

        for (Map<String, Object> order : orderList) {
            // 查询当前推荐中是否包含该商家
            UnionShopRecommend recommendData = unionShopRecommendMapper.selectUnionShopRecommendById(order.get("shop_id").toString());
            if (recommendData != null) {
                continue;
            }

            UnionShopRecommend recommend = UnionShopRecommend.of()
                    .setShopId(order.get("shop_id").toString())
                    .setSort(sort + 5 + i)
                    .setDueDate(new Date())
                    .setCreatedTime(new Date())
                    .setCreatedUser(userId)
                    .setStatus("1");

            unionShopRecommendMapper.insertUnionShopRecommend(recommend);

            i++;
        }

        return true;
    }
}
